package org.scboy.dbms.service.imp;

import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.BaseDictionaryInfoDao;
import org.scboy.dbms.dao.BaseDictionaryTypeValueDao;
import org.scboy.dbms.service.DictionaryTypeValueService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.lang.UUID;

/**
 * 字典业务类
 * @author yanghao
 *
 */
@Service
public class DictionaryTypeValueServiceImpl implements DictionaryTypeValueService {

	@Autowired
	EntityFactory factory;
	
	/**
	 * 查询字典
	 * @param model 查询参数
	 * @return 结果集合
	 */
	public List<Model> list(Model model){
		
		BaseDictionaryTypeValueDao dao=factory.getDao(BaseDictionaryTypeValueDao.class);
		
		return dao.queryList(model);
	}

	/**
	 * 查询字典
	 * @param model 查询参数
	 * @return 结果集合
	 */
	public Page page(Model model){
		BaseDictionaryTypeValueDao dao=factory.getDao(BaseDictionaryTypeValueDao.class);
		return dao.queryPage(model);
	}

	@Transactional
	public boolean add(String typeCode, String infoId,String value) {
		
		BaseDictionaryInfoDao infoDao=factory.getDao(BaseDictionaryInfoDao.class);
		BaseDictionaryTypeValueDao dao=factory.getDao(BaseDictionaryTypeValueDao.class);
		
		//删除掉原有的关联
		Model query=Model.create().add("typeCode", typeCode);
		query.add("infoId", infoId);
		dao.delete(query);
		Model dInfo=infoDao.get(Model.create().add("id", infoId));
		Model typeValue=Model.create();
		typeValue.add("id", UUID.randomUUID().toString());
		typeValue.add("infoId", infoId);
		typeValue.add("typeCode", typeCode);
		typeValue.add("code", dInfo.getString("code"));
		typeValue.add("dicValue", value);
		int count= dao.insert(typeValue);
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}
	
	public Model get(String typeCode, String infoId) {
		BaseDictionaryTypeValueDao dao=factory.getDao(BaseDictionaryTypeValueDao.class);
		Model data=dao.get(Model.create().add("typeCode", typeCode).add("infoId", infoId));
		if(data==null) {
			data=Model.create()
					.add("typeCode", typeCode)
					.add("infoId", infoId)
					.add("value", "");
		}else {
			data=Model.create()
					.add("typeCode", data.getString("typeCode"))
					.add("infoId", data.getString("infoId"))
					.add("value", data.getString("dicValue"));
		}
		return data;
	}
	
	@Transactional
	public boolean initTypeValue(String typeCode, String code) {
		BaseDictionaryInfoDao infoDao=factory.getDao(BaseDictionaryInfoDao.class);
		BaseDictionaryTypeValueDao dao=factory.getDao(BaseDictionaryTypeValueDao.class);
		//删除掉原有的关联
		Model query=Model.create().add("typeCode", typeCode);
		query.add("code", code);
		dao.delete(query);
		List<Model> model=infoDao.select(Model.create().add("code", code));
		for (Model info : model) {
			Model typeValue=Model.create();
			typeValue.add("id", UUID.randomUUID().toString());
			typeValue.add("infoId", info.getString("id"));
			typeValue.add("typeCode", typeCode);
			typeValue.add("code", code);
			typeValue.add("dicValue", info.getString("dicValue"));
			dao.insert(typeValue);
		}
		return true;
	}
	
	@Transactional
	public boolean clearTypeValue(String typeCode, String code) {
		BaseDictionaryTypeValueDao dao=factory.getDao(BaseDictionaryTypeValueDao.class);
		//删除掉原有的关联
		Model query=Model.create().add("typeCode", typeCode);
		query.add("code", code);
		dao.delete(query);
		return true;
	}
}


